/**
 * 商品经营范围维护
 * @author shixc
 * @date 2019/01/31
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_gwda = {},
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'GWDA004', width: 120, title: '单据日期', fixed: 'left', sort: true}
            , {field: 'GWDA002', width: 80, title: '客户', search: 'default', searchOpts: {icon: true}, sort: true}
            , {field: 'GWAKL004', width: 120, title: '客户名称'}
            , {field: 'GWDA006', width: 80, title: '是否包含',
                filterDatas: app.datas_('crm_gwai501_gwax011')
            }
            , {field: 'GWDA007_NAME', width: 80, title: '产品系列'}
            , {field: 'GWDA003', width: 80, title: '品号', search: 'default', searchOpts: {icon: true}, sort: true}
            , {field: 'GWAML003', width: 120, title: '品名'}
            , {field: 'GWAML004', width: 100, title: '规格'}
            , {
                field: 'GWDA005', width: 80, title: '纳入年度指标否',
                filterDatas: app.datas_('crm_gwai501_gwax011')
            }
            , {
                field: 'GWDAACTI', width: 80, title: '有效码',
                filterDatas: app.datas_('sys_y_n')
            },
            {
                width: 60,
                title: '',
                toolbar: '#script-gwdc001setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwdc001SettingAction, gwdc001table
    ;

    var Gwdc001 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwdc001',
                _searchs: [{
                    id: 'gwdc001', tableId: 'gwdc001_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwdc001_edit', tpl: '/gwdc001_edit'
                }, {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }, {
                    dom: 'gwdc001_auto', tpl: '/gwdc001_auto'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwdc001table = table.render({
                    elem: app.getByid(doc, 'gwdc001_table'),
                    url: g_ctxPath + 'crm/gwdc001/getList',
                    exportsUrl: g_ctxPath + 'crm/gwdc001/exportsData',
                    exportsName: '商品经营范围数据',
                    importsUrl: g_ctxPath + 'crm/gwdc001/importsData',
                    exportsMbUrl: g_ctxPath + 'files/商品经营范围维护.xlsx',
                    title: '商品经营范围信息表',
                    id: 'gwdc001_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],//, 'exports_import'
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: table_gwda,
                    done: function (res, curr, count) {
                        var table_data = new Array();
                        var ids =new Array();
                        table_data = res.data;

                        //在缓存中找到id ,然后设置data表格中的选中状态
                        //循环所有数据，找出对应关系，设置checkbox选中状态
                        for (var i = 0; i < res.data.length; i++) {
                            for (var j = 0; j < ids.length; j++) {
                                //数据id和要勾选的id相同时checkbox选中
                                if (res.data[i].id == ids[j]) {
                                    //这里才是真正的有效勾选
                                    res.data[i]["LAY_CHECKED"] = 'true';
                                    //找到对应数据改变勾选样式，呈现出选中效果
                                    var index = res.data[i]['LAY_TABLE_INDEX'];
                                    $('.layui-table-fixed-l tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                                    $('.layui-table-fixed-l tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                                }
                            }
                        }
                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwdc001setting-action').innerHTML).render({}, function (string) {
                    gwdc001SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwdc001_table)', function (obj) {

                    layer.tips(gwdc001SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwdc001_edit',
                                            tagData: obj,
                                            index: 0,
                                            openOpts: {
                                                title: '商品经营范围编辑',
                                                area: ['720px', '520px']
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.del(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwdc001_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwdc001_edit',
                        tagData: obj,
                        index: 0,
                        openOpts: {
                            title: '商品经营范围编辑',
                            area: ['720px', '520px']
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwdc001_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwdc001_edit',
                                tagData: {
                                    is_add: true
                                },
                                openOpts: {
                                    title: '商品经营范围新增',
                                    area: ['720px', '520px']
                                },
                                index: 0
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;
                        case 'batch':
                            self._openTplFull({
                                tag: 'gwdc001_auto',
                                tagData: {
                                    is_add: true
                                },
                                openOpts: {
                                    title: '批次产生',
                                    area: ['900px', '650px']
                                },
                                index: 2
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwdc001_table', where: table_gwda});
        },

        del: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwdc001/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '商品经营范围信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        deleteDataBatch: function () {
            var checkStatus = table.checkStatus('gwdc001_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWDA');
                    _temp.GWDAENT = v.GWDAENT;
                    _temp.GWDA001 = v.GWDA001;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwdc001/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '商品经营范围信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwdc001',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWDA003') {//产品开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwam001',
                                            columnList: [
                                                {label: '产品编号', prop: 'GWAM001', width: '150'},
                                                {label: '产品名称', prop: 'GWAML003', width: '150'},
                                                {label: '规格', prop: 'GWAML004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDA003 = data.GWAM001;
                                        },
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择产品',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'GWDA002') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwak001',
                                            columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                            //url: 'admin/getGwakData'
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDA002 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwdc001();

});